package com.zmn.oms.third.dj58;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：58到家接口使用的常量类
 *
 * @author xiewenbing
 * @date 2019/11/01 11:300
 */
@Component
public final class DaoJia58Consts {
    /**
     * 58到家渠道名称
     */
    public static final String CHANNEL_NAME = "58到家-直约API";
    public static final String CHANNEL_NAME_APARTMENT = "58到家-同城公寓";
    public static final String REWORK_CHANNEL_NAME = "58到家返修单";

    /**
     * 渠道ID常量
     */
    public static final int CHANNEL_ID_API = 32794;     // 58到家-直约API
    public static final int CHANNEL_ID_APARTMENT = 33979; // 58到家-同城公寓
    public static final int CHANNEL_ID_DJ58_REWORK = 33940;   // 值订单（会员返修订单）

    /**
     * 58到家接入总开关
     */
    public static boolean POWER_ON;

    /**
     * 58到家接入参数
     */
    public static String APPKEY;
    public static String SECRET;
    public static String TOKEN;

    /**
     * 58到家接入Url
     */
    public static String API_URL;

    /**
     * 58到家增值订单同步时，用于标识我方身份的识别码
     */
    public static Long ORG_ID;

    /**
     * 58到家发送消息协议的格式
     */
    public static final String DJ58_MSG_FORMAT = "json";

    /**
     * 接口地址
     */
    public static final String GET_TOKERN_PATH = "v1/oauth/getAccessToken";     // 获取token
    public static final String ACCEPT_ORDER_PATH = "agreement/acceptOrder"; // 同步接单子路径
    public static final String BEGIN_SERVER_PATH = "agreement/beginServer"; // 同步服务子路径
    public static final String GET_PRICE_LIST_PATH = "agreement/getPriceList";  // 获取价目列表子路径
    public static final String SUPPLY_PAY_PATH = "agreement/supplyPay";     // 获取补充支付链接子路径
    public static final String FINISH_ORDER_PATH = "agreement/finishOrder"; // 同步订单完成子路径
    public static final String CANCEL_ORDER_PATH = "agreement/cancelOrder"; // 同步订单取消子路径
    public static final String REWORK_ORDER_PATH = "insure/updateRepair";   // 同步维修单路径
    public static final String UPDATE_ORDERP_PATH = "agreement/updateOrder";    // 同步订单修改子路径

    /**
     * 订单-消息的功能标识（FunId）
     */
    public static final String CREATE_CLUE_REQUST_FLAG = "createClue";      // 发送创建线索的消息标志
    public static final String CANCEL_ORDER_REQUEST_FLAG = "cancelAgreementOrder";  // 发送取消订单的消息标志
    public static final String REPLENISH_PAY_NOTIFY_FLAG = "replenishPayNotify";    // 发送补充支付通知的消息标志
    public static final String BILL_CHANGE_REQUEST_FLAG = "billChange";     // 发送订单金额发生改变的通知
    public static final String FINISH_ORDER_REQUEST_FLAG = "finishAgreementOrder";  // 发送订单完成的消息标志

    /**
     * 保单-消息的功能标识（FunId）
     */
    public static final String INSURE_CREATE_FLAG = "createInsure"; // 创建保单服务
    public static final String INSURE_CANCEL_FLAG = "cancelInsure"; // 取消保单服务
    public static final String INSURE_APPLY_REPAIR_FLAG = "applyRepair";    // 申请维修

    /**
     * 平台发送给58到家的消息状态码
     */
    public static final int ZMN_RETURN_CODE_SUCCESS = 0;    // 成功
    public static final int ZMN_RETURN_CODE_SERVER_ERROR = 2;   // 服务器内部错误
    public static final int ZMN_RETURN_CODE_PARAM_ERROR = 1001; // 参数错误

    /**
     * 58到家的返回的消息状态码
     */
    public static final int DAOJIA58_RETURN_CODE_SUCCESS = 0;   // 成功
    public static final int DAOJIA58_RETURN_CODE_SERVER_ERROR = 500;    // 服务器内部错误
    public static final int DAOJIA58_RETURN_CODE_PARAM_ERROR = 1001;    // 参数错误
    public static final int DAOJIA58_RETURN_CODE_TOKEN_ERROR = 2001;    // Token未找到（token过期）
    public static final int DAOJIA58_RETURN_CODE_GET_TOKEN_OVER = 2003; // 获取accessToken超过调用次数
    public static final int DAOJIA58_RETURN_CODE_INVALID_SERVICE_KEY = 2002;    // 无效的serviceKey
    public static final int DAOJIA58_RETURN_CODE_ILLEGAL_ORGID = 5001;  // serviceKey和orgId不合法
    public static final int DAOJIA58_RETURN_CODE_PAY_PRICE_EMPTY = 5007;    // 必须要传递补充支付的金额

    /**
     * 同步接单返回的消息状态码
     */
    public static final int DAOJIA58_RETURN_CODE_NO_CLUE = 2041;    // 无线索
    public static final int DAOJIA58_RETURN_CODE_CLUE_CANCEL = 2042;    // 线索已取消
    public static final int DAOJIA58_RETURN_CODE_CLUE_OTHER_ACCEPT = 2043;  // 线索已被接单
    public static final int DAOJIA58_RETURN_CODE_CLUE_ACCEPT_SUCCESS = 2044;    // 接单成功
    public static final int DAOJIA58_RETURN_CODE_CLUE_ACCEPT_FAILED = 2045;     // 接单失败
    public static final int DAOJIA58_RETURN_CODE_CLUE_ACCEPTED = 2046;  // 已经成功接过此单


    /**
     * 58到家发送的订单金额改变类型
     */
    public static final int BILL_CHANGE_TYPE_ORDER_CANCEL = 1;  // 订单取消
    public static final int BILL_CHANGE_TYPE_ORDER_PAY = 2; // 订单支付
    public static final int BILL_CHANGE_TYPE_ORDER_REFUND = 3;  // 订单退款
    public static final int BILL_CHANGE_TYPE_ORDER_FINISH = 4;  // 订单完成

    /**
     * 58到家的接单类型
     */
    public static final int DAOJIA58_ACCEPT_ORDER = 1;  // 接单
    public static final int DAOJIA58_REJECT_ORDER = 2;  // 拒单

    /**
     * 58到家的价格类型
     */
    public static final int PRICE_ITEM_TYPE_FIXED = 1;  // 固定金额
    public static final int PRICE_ITEM_TYPE_CUSTOM = 2; // 自定义

    /**
     * 58到家的取消订单原因
     */
    public static final int DAOJIA58_ORDER_CANCEL_CONSULT = 1;  // 觉得不合适，双方协商后取消
    public static final int DAOJIA58_ORDER_CANCEL_DISACORD_TIME = 2;    // 时间无法达成一致
    public static final int DAOJIA58_ORDER_CANCEL_IMPROPER_SITE = 3;    // 地点不合适
    public static final int DAOJIA58_ORDER_CANCEL_OTHER = 4;    // 其他
    public static final int DAOJIA58_ORDER_CANCEL_TEMP_BUSINESS = 5;    // 临时有事取消预约
    public static final int DAOJIA58_ORDER_CANCEL_APPOINTMENT_BAD = 6;  // 预约信息填写错误
    public static final int DAOJIA58_ORDER_CANCEL_NO_DISTRIBUTE = 7; // 长时间未派单
    public static final int DAOJIA58_ORDER_CANCEL_UNABLE_SERVE = 8; // 派单后小哥无法服务
    public static final int DAOJIA58_ORDER_CANCEL_FINISH = 9;   // 订单完结取消

    /**
     * 错误状态码
     */
    public static final int ERROR_CODE_SERVER_UNKNOWN = 50000;  // 其他错误
    public static final int ERROR_CODE_PARSE_MSG_FAILED = 50001;    // 解析消息内容失败
    public static final int ERROR_CODE_PARAM_INVALID = 50002;   // 传入的参数错误
    public static final int ERROR_CODE_SEND_REQUEST_FAILED = 50003; // 发送消息失败的错误代码

    /**
     * 58到家直约平台其他价目项id
     */
    public static final String OTHER_PRICE_ITEM_IDS = "8000000000";


    /**
     * 线索订单支付类型常量
     */
    public static final int CLUES_PAY_TYPE_NORMAL = 1;    // 普通支付类型
    public static final int CLUES_PAY_TYPE_CREDIT = 2;    // 信用支付类型

    /**
     * 58到家 sell_type 代表渠道
     */
    public static final int DAOJIA58_SELL_TYPE = 4;

    /**
     * 产品id
     */
    public static final int PRODUCT_ID=100002;


    /**
     * 通过渠道ID或线索类型获取渠道信息
     *
     * @param channelId
     * @param cluesPayType
     * @return
     */
    public static ChannelInfo getChannelInfo(Integer channelId, Integer cluesPayType) {
        if (channelId != null) {
            if (Objects.equals(CHANNEL_ID_API, channelId)) {
                return new ChannelInfo(CHANNEL_ID_API, CHANNEL_NAME, false);
            } else if (Objects.equals(CHANNEL_ID_DJ58_REWORK, channelId)) {
                return new ChannelInfo(CHANNEL_ID_DJ58_REWORK, REWORK_CHANNEL_NAME, false);
            } else if (Objects.equals(CHANNEL_ID_APARTMENT, channelId)) {
                return new ChannelInfo(CHANNEL_ID_APARTMENT, CHANNEL_NAME_APARTMENT, true);
            }
        }

        if (Objects.equals(CLUES_PAY_TYPE_CREDIT, cluesPayType)) {
            return new ChannelInfo(CHANNEL_ID_APARTMENT, CHANNEL_NAME_APARTMENT, true);
        }

        return new ChannelInfo(CHANNEL_ID_API, CHANNEL_NAME, false);
    }

    /**
     * 获取该接口接入的所有渠道ID
     *
     * @return
     */
    public static List<Integer> getAllChannelIds() {
        List<Integer> channelIds = new LinkedList<>();
        channelIds.add(CHANNEL_ID_API);
        channelIds.add(CHANNEL_ID_APARTMENT);
        channelIds.add(CHANNEL_ID_DJ58_REWORK);
        return channelIds;
    }


    @Value("${dj58.service.power-on:false}")
    public void setPowerOn(boolean powerOn) {
        POWER_ON = powerOn;
    }

    @Value("${dj58.rework.org-id : 60000000079531}")
    public void setReworkOrgId(Long orgId) {
        ORG_ID = orgId;
    }

    @Value("${dj58.service.key:null}")
    public void setAppkey(String appkey) {
        APPKEY = appkey;
    }

    @Value("${dj58.service.secret:null}")
    public void setSecret(String secret) {
        SECRET = secret;
    }

    @Value("${dj58.service.token:null}")
    public void setToken(String token) {
        TOKEN = token;
    }

    @Value("${dj58.service.url:null}")
    public void setApiUrl(String apiUrl) {
        API_URL = apiUrl;
    }

    /**
     * 渠道信息对象
     */
    @Data
    public static class ChannelInfo {
        /**
         * 渠道ID
         */
        private int id;
        /**
         * 渠道名称
         */
        private String name;
        /**
         * 是否为“同城公寓”订单
         */
        private boolean apartmentOrder;

        /**
         * 构造器
         *
         * @param id
         * @param name
         * @param apartmentOrder
         */
        public ChannelInfo(int id, String name, boolean apartmentOrder) {
            this.id = id;
            this.name = name;
            this.apartmentOrder = apartmentOrder;
        }
    }
}
